#include "sgl_button.h"
#include "stdio.h"
#include "../draw/sgl_draw_icon.h"
#include "../draw/sgl_draw_font.h"

int sgl_button_init(sgl_button_t *button, sgl_obj_t* parent)
{
    sgl_obj_t* obj;
    if(parent == NULL) {
        return -1;
    }
    button->text = NULL;
    button->icon = NULL;
    button->font = NULL;
    obj = &button->obj;
    obj->parent = parent;
    obj->obj_type = SGL_OBJ_BUTTON;
    obj->event_data = NULL;
    obj->ev_stat = SGL_EVENT_NORMAL;
    sgl_obj_add_src_act_list(obj);
    sgl_obj_add_dirty_list(obj);
    return 0;
}

sgl_obj_t* sgl_button_create(sgl_obj_t* parent)
{
    int ret;
    sgl_button_t *button;
    if(parent == NULL) {
        return NULL;
    }
    button = (sgl_button_t *)sgl_alloc(sizeof(sgl_button_t));
    if(button == NULL)
        return NULL;
    ret = sgl_button_init(button, parent);
    if(ret) {
        return NULL;
    }
    return  &button->obj;
}

//TODO
void sgl_button_delete(sgl_obj_t* obj)
{

}

void sgl_button_set_toggle_color(sgl_obj_t *obj, sgl_color_t color)
{
    sgl_button_t *button = container_of(obj, sgl_button_t, obj);
    button->toggle_color = color;
}

void sgl_button_set_font(sgl_obj_t *obj, sgl_font_t *font)
{
    sgl_button_t *button = container_of(obj, sgl_button_t, obj);
    button->font = font;
}

void sgl_button_set_text(sgl_obj_t *obj, const char *text)
{
    sgl_button_t *button = container_of(obj, sgl_button_t, obj);
    button->text = text;  
}

void sgl_button_set_icon(sgl_obj_t *obj, sgl_icon_t *icon)
{
    sgl_button_t *button = container_of(obj, sgl_button_t, obj);
    button->icon = icon;
}

void sgl_button_draw(sgl_obj_t* obj)
{
    sgl_widget_draw_rect(rect);
    int _start_x = 0, _start_y = 0;
    sgl_color_t _color;
    sgl_button_t *button = container_of(obj, sgl_button_t, obj);
    sgl_surf_t *surf = sgl_draw_get_active_surf(obj);
    sgl_widget_buffer_valid(obj);
    sgl_draw_buffer_clear(surf, obj->parent->style.body_color);
    if(obj->ev_stat == SGL_EVENT_PRESSED) {
        if(obj->style.radius == 0) {
            sgl_draw_rect_solid(surf, rect, button->toggle_color);
        }
        else {
            sgl_draw_round_rect_solid(surf, rect, obj->style.radius, button->toggle_color, obj->parent->style.body_color);
        }
        _color = button->toggle_color;
    }
    else if(obj->ev_stat == SGL_EVENT_RELEASED || obj->ev_stat == SGL_EVENT_NORMAL) {
        if(obj->style.radius == 0) {
            sgl_draw_rect_solid(surf, rect, obj->style.body_color);
        }
        else {
            sgl_draw_round_rect_solid(surf, rect, obj->style.radius, obj->style.body_color, obj->parent->style.body_color);
        }
        _color = obj->style.body_color;
    }
    else {
        //TODO: add other event type
    }
    if(button->icon) {
        if(button->text == NULL || button->font == NULL) {
            _start_x = sgl_widget_draw_ofs_x((obj->size.w - button->icon->width)/2);
            _start_y = sgl_widget_draw_ofs_y((obj->size.h - button->icon->height)/2);
            sgl_draw_icon(surf, _start_x, _start_y, obj->style.text_color, _color, button->icon);
        }
        else {
            int text_width = sgl_text_width(button->font, button->text) + 4;
            _start_x = sgl_widget_draw_ofs_x((obj->size.w - button->icon->width - text_width)/2);
            _start_y = sgl_widget_draw_ofs_y((obj->size.h - button->icon->height)/2);
            sgl_draw_icon(surf, _start_x, _start_y, obj->style.text_color, _color, button->icon);
            _start_y = sgl_widget_draw_ofs_y((obj->size.h - sgl_text_height(button->font))/2);
            sgl_draw_font_string(surf, _start_x + button->icon->width + 4, _start_y, button->text, obj->style.text_color, _color, button->font);
        }
    }
    else {
        if(button->text && button->font) {
            int text_width = sgl_text_width(button->font, button->text);
            _start_x = sgl_widget_draw_ofs_x((obj->size.w - text_width)/2);
            _start_y = sgl_widget_draw_ofs_y((obj->size.h - sgl_text_height(button->font))/2);
            sgl_draw_font_string(surf, _start_x, _start_y, button->text, obj->style.text_color, _color, button->font); 
        }
    }
    sgl_draw_obj_selected(obj, surf, rect);
    sgl_widget_draw_buffer_flush(obj, surf);
}


void sgl_button_event_cb(sgl_obj_t *obj)
{
    sgl_obj_event_cb(obj);
    sgl_button_draw(obj);
}
